package net.chenlin.dp.modules.mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

/**
 * @author Yangsm
 * @ClassName: 发布方
 * @Description: TODO
 * @date 2024年06月14日 20:32
 */
public class PublishSample {

//    public static void main(String[] args) {
//
//        String topic = "yangsm";
//        String content = "你好，ysm的笔记本给你发了条消息呀！！！！！！！！！！！";
//        int qos = 1;
//        String broker = "tcp://broker.emqx.io:1883";
//        String userName = "admin";
//        String password = "password";
//        String clientId = "pubClient";
//        // 内存存储
//        MemoryPersistence persistence = new MemoryPersistence();
//
//        try {
//            // 创建客户端
//            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
//            // 创建链接参数
//            MqttConnectOptions connOpts = new MqttConnectOptions();
//            // 在重新启动和重新连接时记住状态
//            connOpts.setCleanSession(false);
//            // 设置连接的用户名
//            connOpts.setUserName(userName);
//            connOpts.setPassword(password.toCharArray());
//            // 建立连接
//            System.out.println("连接到 broker: " + broker);
//            sampleClient.connect(connOpts);
//            System.out.println("连接成功.");
//            // 创建消息
//            MqttMessage message = new MqttMessage(content.getBytes());
//            // 设置消息的服务质量
//            message.setQos(qos);
//            // 发布消息
//            System.out.println("向" + topic + "发送消息:" + message);
//            sampleClient.publish(topic, message);
//            // 断开连接
//            sampleClient.disconnect();
//            // 关闭客户端
//            sampleClient.close();
//        } catch (MqttException me) {
//            System.out.println("reason " + me.getReasonCode());
//            System.out.println("msg " + me.getMessage());
//            System.out.println("loc " + me.getLocalizedMessage());
//            System.out.println("cause " + me.getCause());
//            System.out.println("excep " + me);
//            me.printStackTrace();
//        }
//    }

    public static void sendMessage(String topic) {
        String broker = "tcp://1.94.111.92:1883";
        String username = "root";
        String password = "Owendeng123";
        String clientid = "publish_client";
        String content = "{\n" +
                "    \"cmd\": \"all\",\n" +
                "    \"timestamp\": \"2024-10-19T14:55:00Z\"\n" +
                "}";
        int qos = 0;

        try {
            MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence());
            // 连接参数
            MqttConnectOptions options = new MqttConnectOptions();
            // 设置用户名和密码
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            options.setConnectionTimeout(60);
            options.setKeepAliveInterval(60);
            // 连接
            client.connect(options);
            // 创建消息并设置 QoS
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            // 发布消息
            client.publish(topic, message);
            System.out.println("Message published");
            System.out.println("发布方的 topic: " + topic);
            System.out.println("发布方发送的 message content: " + content);
            // 关闭连接
            client.disconnect();
            // 关闭客户端
            client.close();
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {

        String broker = "tcp://1.94.111.92:1883";
        String topic = "yangsmTop";
        String username = "root";
        String password = "Owendeng123";
        String clientid = "yangsmpush_client";
        String content = "测试删除clent";
        int qos = 0;

        try {
            MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence());
            // 连接参数
            MqttConnectOptions options = new MqttConnectOptions();
            // 设置用户名和密码
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            options.setConnectionTimeout(60);
            options.setKeepAliveInterval(60);
            // 连接
            client.connect(options);
            // 创建消息并设置 QoS
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            // 发布消息
            client.publish(topic, message);
            System.out.println("Message published");
            System.out.println("topic: " + topic);
            System.out.println("message content: " + content);
            // 关闭连接
            client.disconnect();
            // 关闭客户端
            client.close();
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }
}
